
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/common/index.dart';
import 'package:provider/provider.dart';
import '../provide/cart_provider.dart';
import './cart_page/cart_item.dart';
import './cart_page/cart_bottom.dart';

class CartPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _CartPageState();
  }
}

class _CartPageState extends State<CartPage> with AfterLayoutMixin {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('购物车'),
      ),
      body: Container(
        width: ScreenUtil.screenWidth,
        child: Consumer<CartProvider>(
        builder: (context, data, child) {
          print('cart Consumer callback');
          List cartList = data.cartList;
          if (cartList != null) {
            print('cart render');
            return Stack(
              children: <Widget>[
                data.cartList.length == 0
                    ? buildPageEmpty()
                    : ListView.builder(
                  itemCount: cartList.length,
                  itemBuilder: (context, index) {
                    return CartItem(cartList[index]);
                  },
                ),
                Positioned(
                  bottom: 0,
                  left: 0,
                  child: Opacity(
                    opacity: data.cartList.length != 0 ? 1 : 0,
                    child: CartBottom(),
                  ),
                )
              ],
            );
          } else {
            return Column(
              children: <Widget>[
                Text(data.toString()),
                Text(cartList.toString())
              ],
            );
//            return buildLoadingIndicator();
          }
        },
      ),),
    );
  }


  @override
  void afterFirstLayout(BuildContext context) {
    context.read<CartProvider>().getCartInfo();
  }
}
